package com.hx.jvm.jvm.oom;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

/**
 * 测试直接内存溢出
 *
 * @author jxrjc
 * <p>
 * JVM 参数： -Xmx20M -XX:MaxDirectMemorySize=10M
 */
public class DirectMemoryOOM {

    public static final int _1M = 1024 * 1024;

    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InterruptedException {
        Field unsafeFiled = Unsafe.class.getDeclaredFields()[0];
        unsafeFiled.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeFiled.get(null);
        while (true) {
            unsafe.allocateMemory(_1M);

        }
    }

}
